package com.zjmzxfzhl.modules.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zjmzxfzhl.common.base.BaseServiceImpl;
import com.zjmzxfzhl.common.exception.AppException;
import com.zjmzxfzhl.modules.purchase.entity.PurchaseIndent;
import com.zjmzxfzhl.modules.purchase.mapper.PurchaseIndentMapper;
import com.zjmzxfzhl.modules.purchase.service.PurchaseIndentService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Service
 *
 * @author 庄金明
 */
@Service
public class PurchaseIndentServiceImpl extends BaseServiceImpl<PurchaseIndentMapper, PurchaseIndent>
        implements PurchaseIndentService {
    @Override
    public IPage<PurchaseIndent> list(IPage<PurchaseIndent> page, PurchaseIndent purchaseIndent) {
        return page.setRecords(baseMapper.list(page, purchaseIndent));
    }


    @Override
    public PurchaseIndent validPurchaseIndent(PurchaseIndent indent) {
        if (StringUtils.isEmpty(indent.getIndentCode())) {
            indent.setIndentCode("通用缺省订单编号");
        }
        if (!StringUtils.isEmpty(indent.getIndentCode())) {
            PurchaseIndent filter = new PurchaseIndent();
            filter.setIndentCode(indent.getIndentCode());
            PurchaseIndent dbIndent = this.getOne(new QueryWrapper<>(filter));
            if (null == dbIndent) {
                this.save(indent);
                return indent;
            }
            return dbIndent;
        } else {
            throw new AppException("缺少采购订单编号");
        }
    }

    @Override
    public List<PurchaseIndent> listValidIndentWithoutContract() {
        return this.baseMapper.listValidIndentWithoutContract();
    }

    @Override
    public void emptyContractId(Long contractId) {
        this.baseMapper.emptyContractId(contractId);
    }
}
